﻿package code
{
	import caurina.transitions.*;
	import divinity.utils.log.Log;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import gemini.utils.Filters;
	
	public class Nav extends Sprite
	{
		
		private var _mainframe:Core;
		private var _btns:Array;
		private var container:Sprite;
		private var notes:Array;
		private var notclip:MovieClip;
		private var _log:Log = new Log("Navigation bar");
		
		public function Nav(mf:Core)
		{
			_mainframe = mf;
		
		}
		
		public function init()
		{
			
			container = new Sprite();
			container.x = 10;
			container.y = 580;
			addChild(container);
			
			_btns = [];
			var a = 0;
			if (_mainframe.USER.permission != 2)
			{
				_mainframe.admin.x = 0;
				_mainframe.admin.y = 0;
				_mainframe.admin.mainframe = _mainframe;
				_mainframe.admin.path = _mainframe.path;
				_mainframe.admin.init();
				
				container.addChild(_mainframe.admin);
			}
			addButs();
			//addEventListener(Event.ADDED_TO_STAGE,setListeners);
			//addEventListener(Event.REMOVED_FROM_STAGE,removeListeners);
		}
		
		private function addButs()
		{
			
			var a = 0;
			_btns = [];
			notes = [];
			var z = _mainframe.config.modules.length
			var fistNavMod = false;
			for (var n = 0; n < z; n++)
			{
				if (_mainframe.config.modules[n].note)
				{
					notes.push(n);
				}
				
				if (_mainframe.config.modules[n].pin)
				{
					
					/*_btns[a] = new NavButton(this);
					_btns[a].modinfo = _mainframe.mods[n]
					_btns[a].mynum = a
					container.addChild(_btns[a]);
					_btns[a].init();
					a++;
					        */
					addPinButton(_mainframe.mods[n])
					if (!fistNavMod)
					{
						if (!_mainframe.location)
						{
							fistNavMod = true;
							_mainframe.focusByClip(_mainframe.mods[n]);
								//_mainframe.txt.text="NO LOCATION";
						}
						else
						{
							if (_mainframe.location != null)
							{
								_mainframe.focusOnPos(_mainframe.location.x, _mainframe.location.y);
							}
							
								//_mainframe.txt.text="LOCATION :"+_mainframe.loc.x+":"+_mainframe.loc.y;
						}
						
					}
					
				}
				
			}
			
			addNotes();
			setButtons();
		
		}
		
		public function addPinButton(mod:MovieClip)
		{
			var a:int = _btns.length;
			trace("Button a:", a);
			_btns[a] = new NavButton(this);
			_btns[a].modinfo = mod
			_btns[a].mynum = a
			container.addChild(_btns[a]);
			_btns[a].init();
		
		}
		
		private function addNotes()
		{
			/*if (_mainframe.USER.permission != 1)
			{
				
				if (notes.length > 0)
				{
					notclip = new sysnotes();
					notclip.num.text = notes.length;
					notclip.x = _mainframe.admin.width / 2
					notclip.y = -_mainframe.admin.height
					var scale = 15 / notclip.width;
					notclip.scaleX = scale;
					notclip.scaleY = scale;
					
					_mainframe.admin.addChild(notclip);
						//Filters.addFilters(notclip,"DropShadowFilter,quality:3");
					
				}
			}  */
		}
		
		public function setButtons()
		{
			alignNav()
			var sx
			if (_mainframe.USER.permission != 2)
			{
				sx = _mainframe.admin.width;
			}
			else
			{
				sx = 0;
			}
			
			for (var n = 0; n < _btns.length; n++)
			{
				_btns[n].scaleX = 1
				_btns[n].scaleY = 1
				//_btns[n].x=sx;
				var obj = {x: sx, time: .3,
					
						transition: "easeInOutQuad"}
				if (n == _btns.length - 1)
				{
					trace("zadnji");
					obj.onComplete = alignNav
				}
				Tweener.addTween(_btns[n], obj
					
					)
				_btns[n].sx = _btns[n].x;
				
				//_btns[n].y=580;
				sx += _btns[n].width + 1
			}
		
		}
		
		private function alignNav()
		{
			trace("WIDTH : " + container.width);
			Tweener.addTween(container, {x: 400 - container.width / 2, time: .3, transition: "easeInOutQuad"}
				
				)
		
		}
		
		/*
		 *
		 *
		 *
		 *
		 *
		 *
		 */
		public function overButton(m)
		{
		
		}
		
		public function outButton(m)
		{
		
		}
		
		private function setListeners(e)
		{
			addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
		}
		
		private function removeListeners(e)
		{
			removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
		}
		
		private function mouseMoved(e)
		{
		
		}
		
		public function focusMod(m)
		{
			_mainframe.focusByClip(m)
		}
		
		private function pressMe(e)
		{
			trace(e.target.parent.mynum);
			_mainframe.focusModule(e.target.parent.mynum);
		}
		
		public function resetButtons()
		{
		
		}
		
		public function onMove(me)
		{
			resetButtons()
		
		}
		
		/**
		 * Getters / Setters
		 */
		public function get mainframe():Core
		{
			return _mainframe;
		}
	}

}